package com.cn.ccp.listener;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jfree.util.Log;

import com.cn.ccp.util.CCPContants;

/**
 *
 * @author Administrator
 */
public class RegisterPropertiesUtils {
    private String PROPERTIES_FILE_PATH = "";
    private static final String PROPERTIES_FILE = "register.properties";
    private static final String SERIAL_CODE_KEY = "serial_code_key";

    public static RegisterPropertiesUtils getInstance() {
        RegisterPropertiesUtils registerPropertiesUtils = new RegisterPropertiesUtils();
        registerPropertiesUtils.setPropertiesFilePath(CCPContants.REGIST_PATH);
        return registerPropertiesUtils;
    }

    public String getPropertiesFilePath() {
        return PROPERTIES_FILE_PATH;
    }

    public void setPropertiesFilePath(String PROPERTIES_FILE_PATH) {
        this.PROPERTIES_FILE_PATH = PROPERTIES_FILE_PATH;
    }

    public String getSerialCode() {
        Properties p = getRegisterProperties();
        return p.getProperty(SERIAL_CODE_KEY);
    }

    public void setSerialCode(String serialCode) {

        Properties p = getRegisterProperties();
        p.setProperty(SERIAL_CODE_KEY, serialCode);
        try {
            p.store(new FileOutputStream(getFile()), serialCode);
        } catch (IOException ex) {
            Logger.getLogger(RegisterPropertiesUtils.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private Properties getRegisterProperties() {
        File file = getFile();
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(file));
        } catch (IOException ex) {
            Logger.getLogger(RegisterPropertiesUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return p;
    }

    private File getFile() {
        String fileFullName = PROPERTIES_FILE;
        if (PROPERTIES_FILE_PATH != null || PROPERTIES_FILE_PATH.trim().length() != 0) {
            fileFullName = PROPERTIES_FILE_PATH +File.separator+ PROPERTIES_FILE;
        }
        Log.info("--------fileFullName = "+fileFullName);
        File file = new File(fileFullName);
         if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(RegisterPropertiesUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return file;
    }

}
